import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tot_android_flutter/utils/utils_ext.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:tot_android_flutter/base/BasePage.dart';
import 'package:tot_android_flutter/common/widget/AritcleItemWidget.dart';
import 'package:tot_android_flutter/common/widget/RefreshWrapper.dart';
import 'package:tot_android_flutter/pages/tabpage/home/HomeController.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:tot_android_flutter/pages/DetailsPage.dart';

class HomePage extends BasePage<HomeController> {
  HomePage({Key? key}) : super(key: key);

  final EasyRefreshController _refreshController = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );

  @override
  void preInit() {
    // TODO: implement preInit
    super.preInit();
    // FutureOr Function()
    controller.refreshController = _refreshController;
  }

  @override
  Widget buildBody(BuildContext context) {
    return RefreshWrapper(
        controller: _refreshController,
        onRefresh: () {
          controller.refreshPaging();
        },
        onLoad: () {
          controller.loadMorePaging();
        },
        child: (context, physics) {
          return Obx(() {
            return CustomScrollView(physics: physics, slivers: [
              SliverToBoxAdapter(
                child: Container(
                  color: Colors.black,
                  child: CarouselSlider(
                    options: CarouselOptions(
                      autoPlay: true,
                      viewportFraction: 1,
                      enlargeCenterPage: true,
                      enlargeStrategy: CenterPageEnlargeStrategy.zoom,
                      clipBehavior: Clip.none,
                    ),
                    items: controller.banners.map((item) {
                      return _bannerItemWidget(item);
                    }).toList(),
                  ),
                ),
              ),
              SliverToBoxAdapter(
                child: Container(
                  height: 8.w, // 间隔的高度
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate(
                  (context, index) {
                    var article = controller.articles[index];
                    return _articleItemWidget(article);
                  },
                  childCount: controller.articles.length,
                ),
              ),
            ]);
          });
        });
  }

  Widget _articleItemWidget(item) {
    return AritcleItemWidget(
      article: item,
      onClickCallback: (article) {
        Get.to(DetailsPage(url: article.link, title: article.title));
      },
      onCollectTap: (article) {
        Fluttertoast.showToast(msg: "收藏${article.title}");
      },
    );
  }

  Widget _bannerItemWidget(item) {
    return Container(
        child: Stack(
      children: [
        Center(child: Image.network(item.imagePath)),
        Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Container(
              color: Colors.black45, // 半透明背景
              padding: EdgeInsets.all(6.w),
              child: Text(item.title,
                  textAlign: TextAlign.right,
                  style: TextStyle(color: Colors.white, fontSize: 15.w)),
            ))
      ],
    ));
  }
}
